/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador.autogenerats;

import controlador.autogenerats.exceptions.NonexistentEntityException;
import entity.Tractament;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.ModeAplicacio;
import entity.Produccio;
import entity.Fitosanitari;

/**
 *
 * @author David
 */
public class TractamentJpaController implements Serializable {

    public TractamentJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Tractament tractament) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ModeAplicacio modeAplicacioId = tractament.getModeAplicacioId();
            if (modeAplicacioId != null) {
                modeAplicacioId = em.getReference(modeAplicacioId.getClass(), modeAplicacioId.getId());
                tractament.setModeAplicacioId(modeAplicacioId);
            }
            Produccio produccioId = tractament.getProduccioId();
            if (produccioId != null) {
                produccioId = em.getReference(produccioId.getClass(), produccioId.getId());
                tractament.setProduccioId(produccioId);
            }
            Fitosanitari fitosanitariId = tractament.getFitosanitariId();
            if (fitosanitariId != null) {
                fitosanitariId = em.getReference(fitosanitariId.getClass(), fitosanitariId.getId());
                tractament.setFitosanitariId(fitosanitariId);
            }
            em.persist(tractament);
            if (modeAplicacioId != null) {
                modeAplicacioId.getTractamentCollection().add(tractament);
                modeAplicacioId = em.merge(modeAplicacioId);
            }
            if (produccioId != null) {
                produccioId.getTractamentCollection().add(tractament);
                produccioId = em.merge(produccioId);
            }
            if (fitosanitariId != null) {
                fitosanitariId.getTractamentCollection().add(tractament);
                fitosanitariId = em.merge(fitosanitariId);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Tractament tractament) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tractament persistentTractament = em.find(Tractament.class, tractament.getId());
            ModeAplicacio modeAplicacioIdOld = persistentTractament.getModeAplicacioId();
            ModeAplicacio modeAplicacioIdNew = tractament.getModeAplicacioId();
            Produccio produccioIdOld = persistentTractament.getProduccioId();
            Produccio produccioIdNew = tractament.getProduccioId();
            Fitosanitari fitosanitariIdOld = persistentTractament.getFitosanitariId();
            Fitosanitari fitosanitariIdNew = tractament.getFitosanitariId();
            if (modeAplicacioIdNew != null) {
                modeAplicacioIdNew = em.getReference(modeAplicacioIdNew.getClass(), modeAplicacioIdNew.getId());
                tractament.setModeAplicacioId(modeAplicacioIdNew);
            }
            if (produccioIdNew != null) {
                produccioIdNew = em.getReference(produccioIdNew.getClass(), produccioIdNew.getId());
                tractament.setProduccioId(produccioIdNew);
            }
            if (fitosanitariIdNew != null) {
                fitosanitariIdNew = em.getReference(fitosanitariIdNew.getClass(), fitosanitariIdNew.getId());
                tractament.setFitosanitariId(fitosanitariIdNew);
            }
            tractament = em.merge(tractament);
            if (modeAplicacioIdOld != null && !modeAplicacioIdOld.equals(modeAplicacioIdNew)) {
                modeAplicacioIdOld.getTractamentCollection().remove(tractament);
                modeAplicacioIdOld = em.merge(modeAplicacioIdOld);
            }
            if (modeAplicacioIdNew != null && !modeAplicacioIdNew.equals(modeAplicacioIdOld)) {
                modeAplicacioIdNew.getTractamentCollection().add(tractament);
                modeAplicacioIdNew = em.merge(modeAplicacioIdNew);
            }
            if (produccioIdOld != null && !produccioIdOld.equals(produccioIdNew)) {
                produccioIdOld.getTractamentCollection().remove(tractament);
                produccioIdOld = em.merge(produccioIdOld);
            }
            if (produccioIdNew != null && !produccioIdNew.equals(produccioIdOld)) {
                produccioIdNew.getTractamentCollection().add(tractament);
                produccioIdNew = em.merge(produccioIdNew);
            }
            if (fitosanitariIdOld != null && !fitosanitariIdOld.equals(fitosanitariIdNew)) {
                fitosanitariIdOld.getTractamentCollection().remove(tractament);
                fitosanitariIdOld = em.merge(fitosanitariIdOld);
            }
            if (fitosanitariIdNew != null && !fitosanitariIdNew.equals(fitosanitariIdOld)) {
                fitosanitariIdNew.getTractamentCollection().add(tractament);
                fitosanitariIdNew = em.merge(fitosanitariIdNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tractament.getId();
                if (findTractament(id) == null) {
                    throw new NonexistentEntityException("The tractament with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tractament tractament;
            try {
                tractament = em.getReference(Tractament.class, id);
                tractament.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tractament with id " + id + " no longer exists.", enfe);
            }
            ModeAplicacio modeAplicacioId = tractament.getModeAplicacioId();
            if (modeAplicacioId != null) {
                modeAplicacioId.getTractamentCollection().remove(tractament);
                modeAplicacioId = em.merge(modeAplicacioId);
            }
            Produccio produccioId = tractament.getProduccioId();
            if (produccioId != null) {
                produccioId.getTractamentCollection().remove(tractament);
                produccioId = em.merge(produccioId);
            }
            Fitosanitari fitosanitariId = tractament.getFitosanitariId();
            if (fitosanitariId != null) {
                fitosanitariId.getTractamentCollection().remove(tractament);
                fitosanitariId = em.merge(fitosanitariId);
            }
            em.remove(tractament);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Tractament> findTractamentEntities() {
        return findTractamentEntities(true, -1, -1);
    }

    public List<Tractament> findTractamentEntities(int maxResults, int firstResult) {
        return findTractamentEntities(false, maxResults, firstResult);
    }

    private List<Tractament> findTractamentEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Tractament.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Tractament findTractament(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Tractament.class, id);
        } finally {
            em.close();
        }
    }

    public int getTractamentCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Tractament> rt = cq.from(Tractament.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
